package net.jmchilton.spring.collect;

import java.util.List;

import org.springframework.beans.factory.config.ConstructorArgumentValues;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.BeanDefinitionParserDelegate;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

public class ListTemplateParser extends TemplateBeanParser implements BeanDefinitionParser {
  public ListTemplateParser(String className, String factoryMethodName) {
    this.className = className;
    this.factoryMethodName = factoryMethodName;
  }
  
  @Override
  protected void parseChildren(Element element, ParserContext parserContext, AbstractBeanDefinition definition) {
    BeanDefinitionParserDelegate delegate = parserContext.getDelegate();
    List<?> list = delegate.parseListElement(element, definition);
    ConstructorArgumentValues.ValueHolder valueHolder = new ConstructorArgumentValues.ValueHolder(list);
    valueHolder.setSource(parserContext.extractSource(element));
    definition.getConstructorArgumentValues().addIndexedArgumentValue(0, valueHolder);
  }

}
